package com.xkstream.client.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.xkstream.client.R;
import com.xkstream.client.base.BaseRecycleViewAdapter;
import com.xkstream.client.entity.kt.CommentInfo;
import com.xkstream.client.rx.RxObserver;
import com.xkstream.client.rx.Rxbus;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by xiaoyi on 2017-9-28.
 */

public class CommentAdapter extends BaseRecycleViewAdapter {

    private List<CommentInfo> mDatas;

    public CommentAdapter(Context mContext, List<CommentInfo> data) {
        super(mContext);
        this.mDatas = data;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.layout_comment_item, parent, false);
        ItemHolder holder = new ItemHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        onBindItem((ItemHolder) holder, position);
    }

    private void onBindItem(ItemHolder holder, int pos) {
        CommentInfo data = mDatas.get(pos);
        if (!TextUtils.isEmpty(data.getParent_id())) {
            holder.tvReply.setVisibility(View.VISIBLE);
            holder.tvReplyNick.setVisibility(View.VISIBLE);
        } else {
            holder.tvReply.setVisibility(View.GONE);
            holder.tvReplyNick.setVisibility(View.GONE);
        }
        holder.tvAuthor.setText(data.getNickname());
        holder.tvContent.setText(data.getContent());
        if (!TextUtils.isEmpty(data.getParent_id())) {
            holder.tvReplyNick.setText(data.getParent_nickname());
        } else {
            holder.tvReplyNick.setText("");
        }
        holder.itemView.setOnClickListener((v) -> {
            if (mItemClickListener != null) {
                v.setTag("parent");
                mItemClickListener.onItemClick(v, pos);
            }
        });
        holder.tvReplyNick.setOnClickListener((v)->{
            if (mItemClickListener != null) {
                v.setTag("child");
                mItemClickListener.onItemClick(v, pos);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }


    static class ItemHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tvContent)
        TextView tvContent;
        @BindView(R.id.tvAuthor)
        TextView tvAuthor;
        @BindView(R.id.tvReply)
        TextView tvReply;
        @BindView(R.id.tvReplyNick)
        TextView tvReplyNick;

        public ItemHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
